package com.wyd.resourcetj.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.wyd.resourcetj.dao.IMemberDao;
import com.wyd.resourcetj.dto.MemberDTO;
import com.wyd.resourcetj.model.Member;
import com.wyd.resourcetj.service.IMemberService;
import com.wyd.resourcetj.util.CardUtil;
import com.wyd.resourcetj.util.POIUtil;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
/**
 * MemberController
 */
@RestController
@RequestMapping(path="/member",method = RequestMethod.GET)
public class MemberController {

    @Resource
   private IMemberDao memberDao;

   @Resource
   private IMemberService memberService;



   @ApiOperation(value = "查询所有")

   @GetMapping
       public Map findAll( MemberDTO dto){

        List<Member> list = memberDao.findAll();
		Page<Member> page=memberService.findAllDSQL(dto);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "查询成功");
		map.put("count", page.getNumberOfElements());
		map.put("data", page.getContent());
		
		
		return map;

       }
    


       


   @ApiOperation(value = "添加会员",notes="添加会员")
      
   
   @PostMapping(value="add")
     
     public Member add(@RequestBody Member m) {
         
     m.setCard_no(CardUtil.getCardNo());
     
         return memberDao.save(m);
         
     }



     /* @ApiOperation(value = "根据编号删除会员",notes="根据编号删除会员")   //逻辑删除  
     @ApiImplicitParams({
      @ApiImplicitParam(name="n",value ="0 删除，1 恢复",required=true,paramType ="path"),
     @ApiImplicitParam(name="id",value ="会员编号",required=true,paramType ="path")
     })
     @PutMapping(value = "/{id}/{n}")
       public void del(@PathVariable Integer id,@PathVariable Integer n) {
           
        memberDao.deleteMember(id, n);
            
       
           
       } */


       @ApiOperation(value = "根据编号删除会员",notes="根据编号删除会员")
       @ApiImplicitParam(name="id",value ="会员编号",required=true,paramType ="path")
       @DeleteMapping(value ="/{id}")
         public void del(@PathVariable Integer id) {
             
            memberDao.deleteById(id);
              
         
             
         }


     //Integer id,String name,String sex,Integer category,String swpartment,String profession,String grade,String phone

      @ApiOperation(value="根据编号修改会员信息",notes = "根据编号修改会员信息")
	    @ApiImplicitParams({
		  @ApiImplicitParam(name="id",value ="会员编号",required=true,paramType ="path"),
		  @ApiImplicitParam(name="name",value ="新的会员名",required=true,paramType ="path"),
		  @ApiImplicitParam(name="sex",value ="性别",required=true,paramType ="path"),
		  @ApiImplicitParam(name="category",value ="身份",required=true,paramType ="path"),
          @ApiImplicitParam(name="swpartment",value ="系",required=true,paramType ="path"),
          @ApiImplicitParam(name="profession",value ="专业",required=true,paramType ="path"),
          @ApiImplicitParam(name="grade",value ="年级",required=true,paramType ="path"),
          @ApiImplicitParam(name="phone",value ="手机号",required=true,paramType ="path"),
          @ApiImplicitParam(name="remark",value ="备注",required=true,paramType ="path")
		  
	  })
	  @PutMapping(value = "/edit/{id}/{name}/{sex}/{category}/{swpartment}/{profession}/{grade}/{phone}/{remark}")
	  public int editMember(@PathVariable Integer id,@PathVariable String name,@PathVariable String sex,@PathVariable String category,@PathVariable String swpartment,@PathVariable String profession,@PathVariable String grade,@PathVariable String phone,@PathVariable String remark) {
		  
		  return memberDao.editMember(id,name,sex,category,swpartment,profession,grade, phone,remark);
		  
      }




    
      

     
		
	  
	  @ApiOperation(value = "分页条件查询",notes = "分页条件查询")
      @PostMapping(value ="/page")
    // @RequestMapping(value="findAll.do",method = RequestMethod.GET)
	  public Page<Member> findMemberPage(@RequestBody MemberDTO dto){
		  
		  System.out.println(dto.toString());
		  return memberService.findAllDSQL(dto);
	  }

    @ApiOperation(value = "批量添加",notes = "批量添加")
    @PostMapping(value = "/batch")
    public int batchAdd(){
        //path参数是前台页面上传时的文件名，这里使用本地文件模拟下
        String path = "C:\\Users\\11623\\Desktop\\会员登记表.xlsx";
        List<Member> list = new ArrayList<Member>();
        try {
            list = POIUtil.importMember(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return memberService.addAll(list);
    }


    /* @ApiOperation(value="根据编号修改会员信息",notes = "根据编号修改会员信息")
    @ApiImplicitParams({
      @ApiImplicitParam(name="cardNo",value ="会员卡号",required=true,paramType ="path"),
      @ApiImplicitParam(name="status",value ="会员卡状态",required=true,paramType ="path")
      
  })
  @PutMapping(value = "/{cardNo}/{status}")
  public int editMember(@PathVariable String  cardNo,@PathVariable Integer status) {
      
      return memberDao.updateStatus(cardNo,status);
      
  } */

  @ApiOperation(value = "根据编号删除会员",notes="根据编号删除会员")
          /* @ApiImplicitParams({
           @ApiImplicitParam(name="adm_id",value ="要删除管理员的编号",required=true,paramType ="path")
          }) */
          @GetMapping(value = "deletMore")
            public void deleteMoreMember(@RequestParam(value="adm_id") String adm_id) {
                
                System.out.println(adm_id);
              // 接收包含stuId的字符串，并将它分割成字符串数组
    String[] stuList = adm_id.split(",");
    System.out.println(stuList.toString());
    // 将字符串数组转为List<Intger> 类型
    List<Integer> LString = new ArrayList<Integer>();
    for(String str : stuList){
        LString.add(new Integer(str));
    }

              // 调用service层的批量删除函数
              memberDao.deleteMoreMember(LString);
    //return Msg.success().add("数组", LString);
                 
            
                
            }



    
}